package com.ugoodtech.mdcc.core.domain;

        /* Copyright (C) Ugoodtech, Inc - All Rights Reserved.
         * Unauthorized copying of this file, via any medium is strictly prohibited.
         * Proprietary and confidential.
         * Written by Stone Shaw.
         */

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@ApiModel("部门")
@Entity
@Table(name = "departments")
public class Department extends BaseEntity {
    @ApiModelProperty("部门编号")
    private String name;//名称
    @ApiModelProperty("部门名称")
    private String serialNumber;//编号
    @ApiModelProperty("父级部分")
    private Department parent;
    @ApiModelProperty("子部门")
    private List<Department> children;
    @ApiModelProperty("显示所有层级的名字")
    private String totalName;
    @ApiModelProperty("一级部门的名称")
    private String firstDepartmentName;
    @ApiModelProperty("部门负责人")
    private User leader;
    @ApiModelProperty("负责人姓名")
    private String leaderName;
    @ApiModelProperty("负责人编号")
    private String leaderNum;
    @ApiModelProperty("经销商绑定的维修站")
    private List<Station> stationList;
    @ApiModelProperty("部门类型")
    private Integer type;

    @Transient
    private Long parentDepartmentId;

    public Long getParentDepartmentId() {
        return parentDepartmentId;
    }

    public void setParentDepartmentId(Long parentDepartmentId) {
        this.parentDepartmentId = parentDepartmentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "parent_id")
    public Department getParent() {
        return parent;
    }

    public void setParent(Department parent) {
        this.parent = parent;
    }

    @Transient
    public List<Department> getChildren() {
        return children;
    }

    public void setChildren(List<Department> children) {
        this.children = children;
    }

    @Transient
    public String getTotalName() {
        List<String> totalNameList = new ArrayList<>();
        totalNameList.add(this.getName());
        Department department = this;
        int count = 0;
        while (null != department.getParent() && count < 10) {
            department = department.getParent();
            totalNameList.add(department.getName());
            count++;
        }
        Collections.reverse(totalNameList);
        this.totalName = StringUtils.join(totalNameList, "-");
        return this.totalName;
    }

    public void setTotalName(String totalName) {
        this.totalName = totalName;
    }

    @Transient
    public String getFirstDepartmentName() {
        return firstDepartmentName;
    }

    public void setFirstDepartmentName(String firstDepartmentName) {
        this.firstDepartmentName = firstDepartmentName;
    }

    @ManyToOne
    @JoinColumn(name = "user_leader_id")
    public User getLeader() {
        return leader;
    }

    public void setLeader(User leader) {
        this.leader = leader;
    }

    @Transient
    public String getLeaderName() {
        return leaderName;
    }

    public void setLeaderName(String leaderName) {
        this.leaderName = leaderName;
    }

    @Transient
    public String getLeaderNum() {
        return leaderNum;
    }

    public void setLeaderNum(String leaderNum) {
        this.leaderNum = leaderNum;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    @JoinTable(
            name = "department_station",
            joinColumns = {@JoinColumn(name = "department_id")},
            inverseJoinColumns = @JoinColumn(name = "station_id")
    )
    public List<Station> getStationList() {
        return stationList;
    }

    public void setStationList(List<Station> stationList) {
        this.stationList = stationList;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }
}
